//
//  BVModifyBirthViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/21.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVModifyBirthViewController.h"
#import "KLCPopup.h"

@interface BVModifyBirthViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *birthdayTextField;
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) NSDateFormatter *formatter;
@property (nonatomic, strong) UIView *popView;
@property (nonatomic, strong) KLCPopup *popUp;

@end

@implementation BVModifyBirthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"我的生日"];
    [self setupRightBarButtonItem];
    [self.navItem setEnabled:NO];
    [self.navItem setTitleColor:[UIColor BVLineGrayColor] forState:UIControlStateNormal];
    [self setBirthdayTextFieldKeyboard];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.birthdayTextField setText:self.user.birthday];
//    [self.birthdayTextField becomeFirstResponder];
}

- (void)setBirthdayTextFieldKeyboard {
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    _datePicker = datePicker;
    // 只显示时间
    datePicker.datePickerMode = UIDatePickerModeDate;
    // 显示中文
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    // 监听值得改变
    [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    self.birthdayTextField.inputView = datePicker;
    self.birthdayTextField.inputView.backgroundColor = [UIColor BVBackgroundLightGrayColor];
}

- (void)datePickerValueChanged:(UIDatePicker *)datePicker {
    
    NSString *date = [self.formatter stringFromDate:datePicker.date];
    self.birthdayTextField.text = date;
    if ([self.birthdayTextField.text isEqualToString:self.user.birthday] || self.birthdayTextField.text.length == 0) {
        [self.navItem setEnabled:NO];
        [self.navItem setTitleColor:[UIColor BVLineGrayColor] forState:UIControlStateNormal];
    } else {
        [self.navItem setEnabled:YES];
        [self.navItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}

- (NSDateFormatter *)formatter {
    if (!_formatter) {
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"yyyy-MM-dd";
        [_formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    }
    return _formatter;
}

#pragma mark -
#pragma mark - save
- (void)confirmSaveClick {
    [self.hud show:YES];
    [JDStatusBarNotification showWithStatus:@"正在保存" styleName:JDStatusBarStyleDefault ];
    [self.mineTool modifyUserBirthday:self.birthdayTextField.text success:^{
        [self.hud hide:YES];
        [JDStatusBarNotification showWithStatus:@"保存成功" dismissAfter:2.0 styleName:JDStatusBarStyleSuccess];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"保存失败"];
    }];
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
